package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sl.au.entity.ChanceKeyMan;
import com.sl.au.service.ChanceKeyManService;

@RestController
@RequestMapping(value = "/ui/chanceKeyMan")
public class ChanceKeyManController extends BaseController{
	
	@Autowired
	ChanceKeyManService chanceKeyManService;
	
	/**
	 * 销售信息登记信息关键人物分页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String,Object> getPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String chanceId = request.getParameter("chanceId");
		List<ChanceKeyMan>	chanceKeyMan = chanceKeyManService.getChanceKeyMans(start, length, chanceId, null, null);
		int	count = chanceKeyMan.size();
		map.put("data", chanceKeyMan);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/**
	 * 信息关键人物保存
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Map<String, Object> saveChanceKeyMan(HttpServletRequest request, @RequestBody ChanceKeyMan chanceKeyMan) {
		Map<String, Object> map = new HashMap<String, Object>();
		ChanceKeyMan chancekeyManEdit = chanceKeyManService.getRepository().findOne(chanceKeyMan.getId());
		//String chancekeyManEditYON = chanceKeyMan.getId();
		if(chancekeyManEdit == null){
			chanceKeyManService.getRepository().save(chanceKeyMan);
		}
		else{
			chancekeyManEdit.setKeyManName(chanceKeyMan.getKeyManName());
			chancekeyManEdit.setCallName(chanceKeyMan.getCallName());
			chancekeyManEdit.setPhone(chanceKeyMan.getPhone());
			chancekeyManEdit.setKeyMemo(chanceKeyMan.getKeyMemo());
			chancekeyManEdit.setBusiness(chanceKeyMan.getBusiness());
			chanceKeyManService.getRepository().save(chancekeyManEdit);
		}
		map.put("code", 0);
		return map;
	}
	/**
	 * 信息关键人物删除
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public Map<String, Object> deleteChanceKeyMan(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String keyManIds = request.getParameter("ids");
		String id[]=keyManIds.split(",");
	    List<ChanceKeyMan> keyMans=new ArrayList<ChanceKeyMan>();
	    for(int i=0;i<id.length;i++){
	    	String keyManId = id[i];
	    	ChanceKeyMan keyMan=new ChanceKeyMan();
	    	keyMan.setId(keyManId);
	    	keyMans.add(keyMan);
	    }
	    chanceKeyManService.getRepository().deleteInBatch(keyMans);
		map.put("code", 0);
		return map;
	}
	/**
	 * 信息关键人物删除(Mobile)
	 * @return
	 */
	@RequestMapping( value="{id}",method = RequestMethod.DELETE)
	public Map<String, Object> deleteChanceKeyManMobile(@PathVariable String id) {
		Map<String, Object> map = new HashMap<String, Object>();		
	    chanceKeyManService.getRepository().delete(id);;
		map.put("code", 0);
		return map;
	}
}
